<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import LeftOne from '../components/LeftOne.vue'
import LeftTwo from '../components/LeftTwo.vue'
import LeftThree from '../components/LeftThree.vue'
import CenterOne from '../components/CenterOne.vue'
import CenterTwo from '../components/CenterTwo.vue'
import RightOne from '../components/RightOne.vue'
import RightTwo from '../components/RightTwo.vue'
import RightThree from '../components/RightThree.vue'

// 导入背景图片
import bg1Image from '../assets/bg1.png'
import bg2Image from '../assets/bg2.png'
import bg3Image from '../assets/bg3.png'
import bg4Image from '../assets/bg4.png'

// 背景图片数组
const backgroundImages = [bg1Image, bg2Image, bg3Image, bg4Image]

// 当前背景索引 (0 = bg1, 1 = bg2, 2 = 隐藏)
const currentBackgroundIndex = ref(backgroundImages.length)

// 背景图片的 opacity 值
const backgroundOpacityMin = 0.05
const backgroundOpacityMax = 0.8
const backgroundOpacity = ref(backgroundOpacityMin)

// 用于存储 opacity 动画的定时器
let opacityAnimationTimer = null

// 切换背景图片
function toggleBackground() {
  console.log('切换背景')
  console.log(`backgroundOpacity.value = ${backgroundOpacity.value}`)

  let timeout = 0

  // 判断是否正在显示背景图片
  if (currentBackgroundIndex.value !== backgroundImages.length) {
    // 先隐藏背景图片
    animateOpacity('hidden')
    timeout = 2000
  } else {
    // 显示背景图片
    backgroundOpacity.value = backgroundOpacityMin
  }

  // 3秒后切换背景图片
  setTimeout(() => {
    // 轮换背景：bg1 -> bg2 -> 隐藏 -> bg1...
    currentBackgroundIndex.value =
      (currentBackgroundIndex.value + 1) % (backgroundImages.length + 1)
    // 开始 opacity 动画
    animateOpacity('visible')
  }, timeout)
}

// opacity 动画函数
function animateOpacity(visibilityState) {
  // 如果正在执行 opacity 动画，先清除定时器
  if (opacityAnimationTimer) {
    clearInterval(opacityAnimationTimer)
  }

  // let opacity = 0.01
  const interval = 0.01 // 每次增加的值
  const duration = 3000 // 总时间 3 秒
  const steps = duration / (interval * 1000) // 步数
  const stepTime = duration / steps // 每步的时间间隔

  opacityAnimationTimer = setInterval(() => {
    if (visibilityState === 'hidden') {
      backgroundOpacity.value -= interval
    } else {
      backgroundOpacity.value += interval
    }

    // backgroundOpacity.value = opacity

    // 如果 opacity 是1或者是0
    if (
      backgroundOpacity.value <= backgroundOpacityMin ||
      backgroundOpacity.value >= backgroundOpacityMax
    ) {
      clearInterval(opacityAnimationTimer)
      opacityAnimationTimer = null
      backgroundOpacity.value = Math.min(
        backgroundOpacityMax,
        Math.max(backgroundOpacityMin, backgroundOpacity.value)
      )
    }
  }, stepTime)
}

// 获取当前背景图片URL
const getCurrentBackgroundImage = () => {
  if (currentBackgroundIndex.value < backgroundImages.length) {
    return `url(${backgroundImages[currentBackgroundIndex.value]})`
  }
  return 'none'
}

// 获取按钮文本
const getButtonText = () => {
  if (currentBackgroundIndex.value < backgroundImages.length) {
    return `背景 ${currentBackgroundIndex.value + 1}/${backgroundImages.length}`
  }
  return '显示背景'
}

const activeIndex = ref('1')
const handleSelect = (key, keyPath) => {
  console.log(key, keyPath)
}

// 定义一个响应式变量来存储当前时间
const currentTime = ref(getCurrentDateTime())

function getCurrentDateTime() {
  const now = new Date()
  const year = now.getFullYear()
  const month = (now.getMonth() + 1).toString().padStart(2, '0')
  const day = now.getDate().toString().padStart(2, '0')
  const hours = now.getHours().toString().padStart(2, '0')
  const minutes = now.getMinutes().toString().padStart(2, '0')
  const seconds = now.getSeconds().toString().padStart(2, '0')
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

let timer = null

// 在组件挂载时启动定时器
onMounted(() => {
  timer = setInterval(() => {
    currentTime.value = getCurrentDateTime()
  }, 1000)
})

// 在组件卸载时清除所有定时器
onBeforeUnmount(() => {
  // 清除时间更新定时器
  if (timer) {
    clearInterval(timer)
    timer = null
  }

  // 清除 opacity 动画定时器
  if (opacityAnimationTimer) {
    clearInterval(opacityAnimationTimer)
    opacityAnimationTimer = null
  }
})
</script>

<template>
  <div class="home-wrapper">
    <!-- 背景层 -->
    <div
      class="background-layer"
      :style="{
        backgroundImage: getCurrentBackgroundImage(),
        opacity: backgroundOpacity
      }"
    ></div>

    <!-- 内容层 -->
    <div class="content-layer">
      <header class="top">
        <dv-border-box-11 title="视频监控主控屏">
          <!-- <div class="name">创作人：Yunzhh.com</div> -->
          <!-- <div class="time">当前时间：{{ currentTime }}</div> -->
          <!-- 添加切换背景的按钮 -->
          <div class="toggle-bg-btn">
            <button @click="toggleBackground" class="bg-toggle-button">
              {{ getButtonText() }}
            </button>
          </div>

          <!-- 添加切换背景的按钮 -->
          <!-- <div class="toggle-bg-btn">
          <el-menu
            :default-active="activeIndex"
            class="el-menu-demo"
            mode="horizontal"
            :ellipsis="false"
            @select="handleSelect"
          >
            <el-menu-item index="1">Processing Center</el-menu-item>
            <el-sub-menu index="2">
              <template #title>Workspace</template>
              <el-menu-item index="2-1">item one</el-menu-item>
              <el-menu-item index="2-2">item two</el-menu-item>
              <el-menu-item index="2-3">item three</el-menu-item>
              <el-sub-menu index="2-4">
                <template #title>item four</template>
                <el-menu-item index="2-4-1">item one</el-menu-item>
                <el-menu-item index="2-4-2">item two</el-menu-item>
                <el-menu-item index="2-4-3">item three</el-menu-item>
              </el-sub-menu>
            </el-sub-menu>
          </el-menu>
        </div> -->
        </dv-border-box-11>
      </header>

      <main class="bottom">
        <div class="left">
          <dv-border-box-1 class="left-item">
            <LeftOne />
          </dv-border-box-1>
          <dv-border-box-8 class="left-item">
            <LeftTwo />
          </dv-border-box-8>
          <dv-border-box-1 class="left-item">
            <LeftThree />
          </dv-border-box-1>
        </div>

        <div class="center">
          <dv-border-box-8 class="center-top">
            <div class="center-top-item">
              <div class="center-top-item-span">
                <span>当前需求数量</span>
                <CenterOne />
              </div>

              <div class="line"></div>

              <div class="center-top-item-span">
                <span>预计需求数量</span>
                <CenterOne />
              </div>
            </div>
          </dv-border-box-8>
          <dv-border-box-1 class="center-bottom">
            <CenterTwo />
          </dv-border-box-1>
        </div>

        <div class="right">
          <dv-border-box-1 class="right-item">
            <RightOne />
          </dv-border-box-1>

          <dv-border-box-1 class="right-item">
            <RightTwo />
          </dv-border-box-1>

          <dv-border-box-8 :color="['rgb(140,255,181)', 'white']" reverse="true" class="right-item">
            <RightThree />
          </dv-border-box-8>
        </div>
      </main>
    </div>
  </div>
</template>

<style scoped lang="less">
html,
body {
  // height: 100%;
  height: calc(100% - 20px);
  overflow: hidden;
  margin: 0;
  padding: 0;
}

.home-wrapper {
  padding: 0.5%;
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  position: relative;
  box-sizing: border-box; /* 添加此行 */
}

.background-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 1);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  background-attachment: fixed;
  z-index: -1;
}

.content-layer {
  flex: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  color: #fff;
  position: relative;
  z-index: 1;
  overflow: hidden;
  box-sizing: border-box; /* 添加此行 */
}

// 顶部
.top {
  height: 9vh;
  min-height: 60px;
  max-height: 120px;
  .top-nav {
    width: 100%;
    height: 100%;
    position: relative;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
    .name {
      position: absolute;
      left: 100px;
      top: 50%;
      // transform: translateY(-50%);
      color: #00f2ff;
      text-shadow:
        0 0 3px #00f2ff,
        0 0 40px #00f2ff;
      font-weight: 700;
      font-size: 0.9rem;
    }
    .time {
      position: absolute;
      right: 250px;
      top: 50%;
      // transform: translateY(-50%);
      font-weight: 700;
      color: #00f2ff;
      text-shadow:
        0 0 5px #00f2ff,
        0 0 40px #00f2ff;
      font-size: 0.9rem;
    }

    .toggle-bg-btn {
      position: absolute;
      right: 100px;
      top: 45%;
      // transform: translateY(-50%);

      .bg-toggle-button {
        padding: 5px 15px;
        background-color: rgba(0, 242, 255, 0);
        color: #00f2ff;
        border: 0px solid #00f2ff;
        border-radius: 4px;
        font-weight: bold;
        cursor: pointer;
        transition: all 0.3s ease;

        &:hover {
          background-color: rgba(0, 242, 255, 0.4);
          box-shadow: 0 0 10px #00f2ff;
        }
      }
    }
  }
}

.bottom {
  flex: 1;
  display: grid;
  grid-template-columns: 3fr 6fr 3fr;
  gap: 10px;
  min-height: 0;
  padding: 5px;
  box-sizing: border-box; /* 添加此行 */
}

.left {
  display: grid;
  grid-template-rows: repeat(3, 1fr);
  gap: 10px;
  .left-item {
    width: 100%;
    height: 100%;
    border-radius: 8px;
    box-shadow: inset 0 0 15px rgba(0, 98, 255, 0.2);
    background-color: rgba(0, 0, 0, 0.7);
    box-sizing: border-box; /* 添加此行 */
  }
}

.center {
  display: flex;
  width: 100%;
  flex-direction: column;
  justify-content: space-between;

  .center-top {
    width: 100%;
    height: 23%;
    border-radius: 8px;
    box-shadow: inset 0 0 15px rgba(0, 98, 255, 0.2);
    background-color: rgba(0, 0, 0, 0.7);
    box-sizing: border-box; /* 添加此行 */

    .center-top-item {
      display: flex;
      width: 100%;
      height: 100%;
      justify-content: space-around;
      align-items: center;

      .line {
        display: flex;
        width: 5px;
        height: 30%;
        background-color: #3be1c4;
      }

      .center-top-item-span {
        display: flex;
        height: 100%;
        align-items: center;
        justify-content: center;
        flex-wrap: wrap;
        flex-direction: column;
        span {
          display: flex;
          width: 100%;
          justify-content: center;
          align-items: center;
          font-weight: 700;
          color: #00f2ff;
          text-shadow: 0 0 5px #00f2ff;
        }
      }
    }
  }

  .center-bottom {
    width: 100%;
    height: 75%;
    background-color: rgba(0, 0, 0, 0.7);
    border-radius: 8px;
    box-shadow: inset 0 0 15px rgba(0, 98, 255, 0.2);
    box-sizing: border-box; /* 添加此行 */
  }
}

.right {
  display: grid;
  grid-template-rows: repeat(3, 1fr);
  gap: 10px;
  .right-item {
    width: 100%;
    height: 100%;
    border-radius: 8px;
    box-shadow: inset 0 0 15px rgba(0, 98, 255, 0.2);
    background-color: rgba(0, 0, 0, 0.7);
    box-sizing: border-box; /* 添加此行 */
  }
}
</style>
